package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LExamineSort;
import com.learn.core.mapper.LExamineSortMapper;
import com.learn.core.service.ILExamineSortService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LExamineSortServiceImpl
 * @date 2021.04.26 11:38
 */
@Service
public class LExamineSortServiceImpl extends BaseServiceImpl<LExamineSortMapper, LExamineSort> implements ILExamineSortService {
    @Autowired
    LExamineSortMapper lExamineSortMapper;

    @Override
    public List<LExamineSort> queryLExamineSortAll(QueryWrapper<LExamineSort> queryWrapper) {
        List<LExamineSort> treeMenus =new ArrayList<>();
        List<LExamineSort> lExamineSorts = lExamineSortMapper.selectList(queryWrapper);
        // 查询树结构
        for(LExamineSort lExamineSort : getRootNode(lExamineSorts)) {
            lExamineSort=buildChilTree(lExamineSort,lExamineSorts);
            treeMenus.add(lExamineSort);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LExamineSort buildChilTree(LExamineSort pNode,List<LExamineSort> menuList){
        List<LExamineSort> chilMenus =new ArrayList<>();
        for(LExamineSort menuNode : menuList) {
            if(menuNode.getParentNode() == pNode.getExamineSortId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setlExamineSorts(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LExamineSort> getRootNode(List<LExamineSort> menuList) {
        List<LExamineSort> rootMenuLists =new  ArrayList<LExamineSort>();
        for(LExamineSort menuNode : menuList) {
            if(menuNode.getParentNode() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
